package com.servlet.login.root.servlet;

import com.servlet.login.root.bean.User;
import com.servlet.login.root.service.UserService;
import com.servlet.login.root.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebServlet(urlPatterns = "/userlogin", name = "4login", initParams = {
//        @WebInitParam(name = "encoding", value = "utf-8")},
//        loadOnStartup = 1
//)
public class UserServlet extends HttpServlet {

    private UserService userServiceImpl = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("Name");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        session.setAttribute("username",name);
        session.setAttribute("password",password);
        session.setMaxInactiveInterval(1000*60*30);



        String flag = userServiceImpl.returnResults(name, password);
        if ("用户匹配成功！".equals(flag)) {
            User user = userServiceImpl.returnUser(name);
            session.setAttribute("phone",user.getPhone());
            session.setAttribute("email",user.getEmail());
            resp.sendRedirect("/4login/success.jsp");
        } else if ("密码不正确！".equals(flag)) {
            req.getRequestDispatcher("/failure.html").forward(req, resp);
        } else if ("不存在该用户请注册！".equals(flag)) {
            req.getRequestDispatcher("/failure.html").forward(req, resp);
        }

    }
}
